package com.example.ui.activity;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.example.administrator.sharker_demo.R;
import com.example.models.FirstHand;
import com.example.ui.fragment.Coursefragment;
import com.example.ui.fragment.Minefragment;
import com.example.ui.fragment.Studyfragment;

import java.util.ArrayList;

import butterknife.BindView;

import static com.example.App.getAllFragment;

/**
 * 首页
 */
public class MainActivity extends TransitionActivity implements BottomNavigationBar.OnTabSelectedListener {

    @BindView(R.id.layFrame)
    FrameLayout layFrame;
    @BindView(R.id.bottom_navigation_bar)
    BottomNavigationBar bottomNavigationBar;
    private ArrayList<Fragment> fragments;
    public int alldatatype=0;

    public int getAlldatatype() {
        return alldatatype;
    }

    public void setAlldatatype(int alldatatype) {
        this.alldatatype = alldatatype;
    }

    @Override
    public int getlayoutid() {
        return R.layout.activity_main;
    }

    protected void initview() {
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_SHIFTING);
        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_RIPPLE);
        bottomNavigationBar
                .addItem(new BottomNavigationItem(R.drawable.common_study_unselected_3x, "学习").setActiveColorResource(R.color.green))
                .addItem(new BottomNavigationItem(R.drawable.common_course_unselected_3x, "课程").setActiveColorResource(R.color.orange))
                .addItem(new BottomNavigationItem(R.drawable.common_mine_unselected_3x, "我的").setActiveColorResource(R.color.pink))
                .setFirstSelectedPosition(0)
                .initialise();
    }

    @Override
    protected void requestdata() {
        FirstHand.getInstance();
        if (!FirstHand.isHost()) {
            startActivity(new Intent(MainActivity.this, SplashActivity.class));
            finish();
            Toast.makeText(this, "未安装", Toast.LENGTH_SHORT).show();
            return;
        }
    }

    @Override
    protected void viewbehavior() {
        //添加页面
        this.fragments = getfragment();
        //设置默认界面
        setfragmentview(fragments.get(0));
        //点击监听
        bottomNavigationBar.setTabSelectedListener(this);

        getAllFragment().add(fragments.get(0));
    }

    @Override
    protected void gobackfragment(Fragment fragment) {
        setfragmentview(fragment);
    }

    public void setfragmentview(Fragment fragment) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.layFrame, fragment);
        fragmentTransaction.commit();
    }

    public ArrayList<Fragment> getfragment() {
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(new Studyfragment());
        fragments.add(new Coursefragment());
        fragments.add(new Minefragment());
        return fragments;
    }

    @Override
    public void onTabSelected(int position) {
        if (fragments != null && position < fragments.size()) {
            setfragmentview(fragments.get(position));
        }
    }

    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {

    }
}
